﻿Imports System.Data.SqlClient
Imports System.IO
Imports System.Text
Imports lablinkhelper.Utilities
'Server
Public Class clsControls

    Public Shared Function SpGetDeviceList(ByRef pTbl As DataTable) As Boolean
        Dim cmd As New SqlCommand
        Try
            Dim methodConnection As Object
            methodConnection = CType(gv_LabLinkCN, ICloneable).Clone
            Try
                methodConnection.Open()
            Catch ex As Exception
            End Try

            With cmd
                .Connection = methodConnection
                .CommandText = "spGet2WayDeviceList"
                .CommandType = CommandType.StoredProcedure
            End With
            Dim ad As New SqlDataAdapter(cmd)
            ad.Fill(pTbl)
            If pTbl Is Nothing Then Return False
            If pTbl.Rows.Count = 0 Then Return False
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Public Shared Function SpGet2WayDataControl(ByVal pDeviceID As Int32, _
                                         ByVal pTestTypeID As Int32, _
                                         ByVal pTestDate As String, _
                                         ByRef pTbl As DataTable, _
                                         ByRef pTbl1 As DataTable) As Boolean
        Dim cmd As New SqlCommand
        Try

            Dim methodConnection As Object
            methodConnection = CType(gv_LabLinkCN, ICloneable).Clone
            Try
                methodConnection.Open()
            Catch ex As Exception
                File.AppendAllText("C:\DB_Connection_Error.txt", Now.ToString & "   spGet2WayDataControl " & vbCrLf)
            End Try

            With cmd
                .Connection = methodConnection
                .CommandText = "spGet2WayDataControl"
                .CommandType = CommandType.StoredProcedure
                .Parameters.Add("pDeviceID", SqlDbType.Int).Direction = ParameterDirection.Input
                .Parameters("pDeviceID").Value = pDeviceID
                .Parameters.Add("pTestType_ID", SqlDbType.Int).Direction = ParameterDirection.Input
                .Parameters("pTestType_ID").Value = pTestTypeID
                .Parameters.Add("pTestDate", SqlDbType.VarChar, 20).Direction = ParameterDirection.Input
                .Parameters("pTestDate").Value = pTestDate
            End With
            Dim ad As New SqlDataAdapter(cmd)
            Dim ds As New DataSet
            ad.Fill(ds)
            pTbl = ds.Tables(0)
            pTbl1 = ds.Tables(1)
            If pTbl Is Nothing Or pTbl1 Is Nothing Then Return False
            If pTbl.Rows.Count = 0 Or pTbl1.Rows.Count = 0 Then Return False
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Public Shared Function SpUpdateRegTestList(ByVal pBarcode As String, _
                                        ByVal pParaName As String, _
                                        ByVal pTestID As Int64) As Boolean
        Dim cmd As New SqlCommand

        Dim methodConnection As SqlConnection
        methodConnection = lablinkhelper.Utilities.GetDBConnection
        Try
            If methodConnection.State <> ConnectionState.Open Then methodConnection.Open()
        Catch ex As Exception
            File.AppendAllText("C:\DB_Connection_Error.txt", Now.ToString & "     spUpdateRegTestList" & vbCrLf)
            Return False
        End Try

        Try
            With cmd
                .Connection = methodConnection
                .CommandText = "spUpdateRegTestList"
                .CommandType = CommandType.StoredProcedure
                .Parameters.Add("pBarcode", SqlDbType.VarChar, 50).Direction = ParameterDirection.Input
                .Parameters("pBarcode").Value = pBarcode

                .Parameters.Add("pParaName", SqlDbType.NVarChar, 50).Direction = ParameterDirection.Input
                .Parameters("pParaName").Value = pParaName

                .Parameters.Add("pTestID", SqlDbType.BigInt).Direction = ParameterDirection.Input
                .Parameters("pTestID").Value = pTestID
                .ExecuteNonQuery()
            End With
            Return True
        Catch ex As Exception
            File.AppendAllText("C:\DB_Connection_Error.txt", Now.ToString & "     spUpdateRegTestList - - - Lỗi Khác" & vbCrLf & ex.Message & vbCrLf _
                              & pBarcode & vbCrLf _
                              & pParaName & vbCrLf)
            Return False
        End Try
    End Function

    Public Shared Function SpSaveRegTestList(ByVal pBarcode As String, _
                                      ByVal pTestID As Int64, _
                                      ByVal pAlias_Name As String, _
                                      ByVal pPara_Name As String) As Boolean
        Dim cmd As New SqlCommand
        Try
            Dim methodConnection As Object
            methodConnection = CType(gv_LabLinkCN, ICloneable).Clone
            Try
                methodConnection.Open()
            Catch ex As Exception
                File.AppendAllText("C:\DB_Connection_Error.txt", Now.ToString & "   spSaveRegTestList " & vbCrLf)
            End Try

            With cmd
                .Connection = methodConnection
                .CommandText = "spCreateRegList"
                .CommandType = CommandType.StoredProcedure
                .Parameters.Add("pBarcode", SqlDbType.VarChar, 50).Direction = ParameterDirection.Input
                .Parameters("pBarcode").Value = pBarcode
                .Parameters.Add("pTestID", SqlDbType.BigInt).Direction = ParameterDirection.Input
                .Parameters("pTestID").Value = pTestID
                .Parameters.Add("pAlias_Name", SqlDbType.VarChar, 50).Direction = ParameterDirection.Input
                .Parameters("pAlias_Name").Value = pAlias_Name
                .Parameters.Add("pPara_Name", SqlDbType.VarChar, 100).Direction = ParameterDirection.Input
                .Parameters("pPara_Name").Value = pPara_Name
                .ExecuteNonQuery()
            End With
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Public Shared Function SpBuildRequestString(ByVal pBarcode As String, _
                                        ByVal strCode As String, _
                                        ByVal pPortID As Int16, _
                                        Optional ByVal noOfBlock As Byte = 1, _
                                        Optional ByVal rackNo As String = "0001", _
                                        Optional ByVal cupPos As String = "01", _
                                        Optional ByVal sampleNo As String = "0000", _
                                        Optional ByVal blnRerun As Boolean = False) As String
        Dim cmd As New SqlCommand
        Dim strRequest As String = ""
        Dim clsCentaur As New Centaur
        Try
            Dim methodConnection As Object
            methodConnection = CType(gv_LabLinkCN, ICloneable).Clone
            Try
                methodConnection.Open()
            Catch ex As Exception
                File.AppendAllText("C:\DB_Connection_Error.txt", Now.ToString & "   spBuildRequestString " & vbCrLf)
            End Try

            With cmd
                .Connection = methodConnection
                .CommandText = "spBuildRequestString"
                .CommandType = CommandType.StoredProcedure
                .Parameters.Add("pBarcode", SqlDbType.VarChar, 50).Direction = ParameterDirection.Input
                .Parameters("pBarcode").Value = pBarcode
                .Parameters.Add("pPortID", SqlDbType.Int).Direction = ParameterDirection.Input
                .Parameters("pPortID").Value = pPortID
                .Parameters.Add("pPatientName", SqlDbType.VarChar, 200).Direction = ParameterDirection.Output
                .Parameters.Add("pAddress", SqlDbType.VarChar, 200).Direction = ParameterDirection.Output
                .Parameters.Add("pSex", SqlDbType.SmallInt).Direction = ParameterDirection.Output
                .Parameters.Add("pAge", SqlDbType.Int).Direction = ParameterDirection.Output
                .Parameters.Add("pRegDate", SqlDbType.VarChar, 20).Direction = ParameterDirection.Output
                .ExecuteNonQuery()
            End With
            Dim ad As New SqlDataAdapter(cmd)
            Dim dt As New DataTable

            Dim strName, strSex As String
            Dim iCount As Int16
            ad.Fill(dt)
            If dt Is Nothing Then
                If strCode = "CENTAUR" Then
                    Dim strRequestT As String = clsCentaur.CreateTerminationRecord(1, "I")
                    strRequest = clsCentaur.CreateSinglePatientFrame(HeaderRecord, String.Empty, String.Empty, strRequestT)
                    Return strRequest
                Else
                    Return " "
                End If
            End If
            If dt.Rows.Count = 0 Then
                If strCode = "CENTAUR" Then
                    Dim strRequestT As String = clsCentaur.CreateTerminationRecord(1, "I")
                    strRequest = clsCentaur.CreateSinglePatientFrame(HeaderRecord, String.Empty, String.Empty, strRequestT)
                    Return strRequest
                Else
                    Return " "
                End If
            End If
            strName = spGetDatabyLength(cmd.Parameters("pPatientName").Value.ToString.Trim, 16)
            If cmd.Parameters("pSex").Value = 0 Then
                strSex = "F"
            Else
                strSex = "M"
            End If
            Select Case strCode
                Case "AU400"
                    Dim clsAU400 As New AU400
                    Dim strTest() As String
                    ReDim strTest(0 To dt.Rows.Count - 1)
                    For iCount = 0 To dt.Rows.Count - 1
                        strTest(iCount) = dt.Rows(iCount).Item("Alias_name")
                    Next
                    strRequest = clsAU400.CreatOrderString(pBarcode, strTest, rackNo, cupPos, SampleNo, blnRerun)
                    File.AppendAllText("C:\AU400_Request.txt", strRequest & "---" & Now.ToString & vbCrLf)
                Case "AU640"
                    Dim clsAU400 As New AU400
                    Dim strTest() As String
                    ReDim strTest(0 To dt.Rows.Count - 1)
                    For iCount = 0 To dt.Rows.Count - 1
                        strTest(iCount) = dt.Rows(iCount).Item("Alias_name")
                    Next
                    strRequest = clsAU400.CreatOrderString(pBarcode, strTest, rackNo, cupPos, sampleNo, blnRerun)
                    File.AppendAllText("C:\AU640_Request.txt", strRequest & "---" & Now.ToString & vbCrLf)
                Case "CA550"
                    Dim clsCA550 As New CA550
                    Dim strTest() As String
                    ReDim strTest(0 To dt.Rows.Count - 1)
                    For iCount = 0 To dt.Rows.Count - 1
                        strTest(iCount) = dt.Rows(iCount).Item("Alias_name")
                    Next
                    strRequest = clsCA550.CreatOrderString(pBarcode, strTest, rackNo, cupPos)
                    File.AppendAllText("C:\AU400_Request.txt", strRequest & "---" & Now.ToString & vbCrLf)
                Case "AU480"
                    Dim clsAU480 As New AU480
                    Dim strTest() As String
                    ReDim strTest(0 To dt.Rows.Count - 1)
                    For iCount = 0 To dt.Rows.Count - 1
                        strTest(iCount) = dt.Rows(iCount).Item("Alias_name")
                    Next
                    strRequest = clsAU480.CreatOrderString(pBarcode, strTest, rackNo, cupPos, SampleNo, blnRerun)
                    File.AppendAllText("C:\AU400_Request.txt", strRequest & "---" & Now.ToString & vbCrLf)

                Case "ADVIA1650"
                    Dim clsADVIA1650 As New Advia1650
                    Dim strTest() As String
                    ReDim strTest(0 To dt.Rows.Count - 1)
                    For iCount = 0 To dt.Rows.Count - 1
                        strTest(iCount) = dt.Rows(iCount).Item("Alias_name")
                    Next
                    FrameNo = (FrameNo + 1) Mod 8
                    strRequest = clsADVIA1650.CreateSinglePatientFrame(clsADVIA1650.CreatOrderRecord(FrameNo, pBarcode, strTest), FrameNo, noOfBlock)
                    File.AppendAllText("C:\Advia1650_Request.txt", Now.ToString & vbCrLf & strRequest & vbCrLf & vbCrLf)
                Case "CENTAUR"
                    Dim strRequestP, strRequestO, strRequestT, strTest() As String
                    ReDim strTest(0 To dt.Rows.Count - 1)
                    For iCount = 0 To dt.Rows.Count - 1
                        strTest(iCount) = [String].Concat("^^^", dt.Rows(iCount).Item("Alias_name"))
                    Next
                    'File.AppendAllText("C:\BuildQuest_Centaur_" & Now.Month.ToString & Now.Day.ToString & ".log", "|3")
                    strRequestO = clsCentaur.CreatOrderRecord(1, pBarcode, String.Empty, String.Empty, strTest)
                    strRequestP = clsCentaur.CreatePatientRecord(1, "", strName, "", "", String.Empty, strSex, "", String.Empty)
                    strRequestT = clsCentaur.CreateTerminationRecord(1, "F")
                    'File.AppendAllText("C:\Centaur_Request.txt", Now.ToString & vbCrLf & strRequestT & vbCrLf & vbCrLf)
                    strRequest = clsCentaur.CreateSinglePatientFrame(HeaderRecord, strRequestP, strRequestO, strRequestT)
                    File.AppendAllText("C:\Centaur_Request.txt", Now.ToString & vbCrLf & strRequest & vbCrLf & vbCrLf)
                Case "AXSYM"
                    Dim strRequestP, strRequestO, strRequestT, strTest() As String
                    Const strAxsymHeaderRecord As String = "H|\^&||||||||||P|1"
                    ReDim strTest(0 To dt.Rows.Count - 1)
                    Dim clsAXSYM As New AxSYM
                    For iCount = 0 To dt.Rows.Count - 1
                        strTest(iCount) = [String].Concat("^^^", dt.Rows(iCount).Item("Alias_name"))
                    Next
                    strRequestO = clsAXSYM.CreatOrderRecord(1, pBarcode, String.Empty, String.Empty, strTest)
                    strRequestP = clsAXSYM.CreatePatientRecord(1, "", strName)
                    strRequestT = clsAXSYM.CreateTerminationRecord(1, "F")
                    strRequest = clsAXSYM.CreateSinglePatientFrame(strAxsymHeaderRecord, strRequestP, strRequestO, strRequestT)
                    File.AppendAllText("C:\Axsym_Request.txt", Now.ToString & vbCrLf & strRequest & vbCrLf & vbCrLf)
            End Select
            Return strRequest

        Catch ex As Exception
            'Dim f As File
            File.AppendAllText("C:\BuildRequest_Exception.txt", Now.ToString & "----" & strCode & vbCrLf & ex.ToString & vbCrLf & vbCrLf)
            If strCode = "CENTAUR" Then
                Dim strRequestT As String = clsCentaur.CreateTerminationRecord(1, "I")
                strRequest = clsCentaur.CreateSinglePatientFrame(HeaderRecord, String.Empty, String.Empty, strRequestT)
                Return strRequest
            Else
                Return " "
            End If
        End Try
    End Function
    Public Shared Function SpBuildRequestStringCA550(ByVal pBarcode As String, _
                                        ByVal pPortID As Int16, _
                                        Optional ByVal noOfBlock As Byte = 1, _
                                        Optional ByVal rackNo As String = "0001", _
                                        Optional ByVal cupPos As String = "01") As String
        Dim cmd As New SqlCommand
        Dim strRequest As String = ""
        Try
            Dim methodConnection As Object
            methodConnection = CType(gv_LabLinkCN, ICloneable).Clone
            Try
                methodConnection.Open()
            Catch ex As Exception
                File.AppendAllText("C:\DB_Connection_Error.txt", Now.ToString & "   spBuildRequestString " & vbCrLf)
            End Try

            With cmd
                .Connection = methodConnection
                .CommandText = "spBuildRequestString"
                .CommandType = CommandType.StoredProcedure
                .Parameters.Add("pBarcode", SqlDbType.VarChar, 50).Direction = ParameterDirection.Input
                .Parameters("pBarcode").Value = pBarcode
                .Parameters.Add("pPortID", SqlDbType.Int).Direction = ParameterDirection.Input
                .Parameters("pPortID").Value = pPortID
                .Parameters.Add("pPatientName", SqlDbType.VarChar, 200).Direction = ParameterDirection.Output
                .Parameters.Add("pAddress", SqlDbType.VarChar, 200).Direction = ParameterDirection.Output
                .Parameters.Add("pSex", SqlDbType.SmallInt).Direction = ParameterDirection.Output
                .Parameters.Add("pAge", SqlDbType.Int).Direction = ParameterDirection.Output
                .Parameters.Add("pRegDate", SqlDbType.VarChar, 20).Direction = ParameterDirection.Output
                .ExecuteNonQuery()
            End With
            Dim ad As New SqlDataAdapter(cmd)
            Dim dt As New DataTable

            Dim strName As String = Space(11)
            Dim iCount As Int16
            Dim clsCA550 As New CA550
            Dim strTest() As String

            ad.Fill(dt)
            If dt Is Nothing Or dt.Rows.Count = 0 Then

                ReDim strTest(0)
                strTest(0) = "000"

            Else

                strName = SpGetDatabyLength(cmd.Parameters("pPatientName").Value.ToString.Trim, 16)
                ReDim strTest(0 To dt.Rows.Count - 1)
                For iCount = 0 To dt.Rows.Count - 1
                    strTest(iCount) = dt.Rows(iCount).Item("Alias_name")
                Next

            End If
            strRequest = clsCA550.CreatOrderString(pBarcode, strTest, rackNo, cupPos)
            File.AppendAllText("C:\CA550_Request.txt", strRequest & "---" & Now.ToString & vbCrLf)
            Return strRequest

        Catch ex As Exception
            'Dim f As File
            File.AppendAllText("C:\BuildRequest_Exception.txt", Now.ToString & "----" & " CA550 " & vbCrLf & ex.ToString & vbCrLf & vbCrLf)

        End Try
    End Function
    Public Shared Function SpBuildRequestStringElecsys(ByVal pBarcode As String, _
                                      ByVal pPortID As Int16, _
                                      ByVal DP As String, _
                                      Optional ByVal NoOfBlock As Byte = 1) As String
        Dim cmd As New SqlCommand
        Dim clsElecsys As New ELECSYS2010
        Dim strRequest As String = ""
        Try

            Dim methodConnection As Object
            methodConnection = CType(gv_LabLinkCN, ICloneable).Clone
            Try
                methodConnection.Open()
            Catch ex As Exception
                File.AppendAllText("C:\DB_Connection_Error.txt", Now.ToString & "   spBuildRequestStringElecsys " & vbCrLf)
            End Try

            File.AppendAllText("C:\BuildREQuest_ELECSYS2010.txt", Now.ToString & vbCrLf & pBarcode & "|" & "|" & pPortID)
            With cmd
                .Connection = methodConnection
                .CommandText = "spBuildRequestString"
                .CommandType = CommandType.StoredProcedure
                .Parameters.Add("pBarcode", SqlDbType.VarChar, 50).Direction = ParameterDirection.Input
                .Parameters("pBarcode").Value = pBarcode
                .Parameters.Add("pPortID", SqlDbType.Int).Direction = ParameterDirection.Input
                .Parameters("pPortID").Value = pPortID
                .Parameters.Add("pPatientName", SqlDbType.VarChar, 200).Direction = ParameterDirection.Output
                .Parameters.Add("pAddress", SqlDbType.VarChar, 200).Direction = ParameterDirection.Output
                .Parameters.Add("pSex", SqlDbType.SmallInt).Direction = ParameterDirection.Output
                .Parameters.Add("pAge", SqlDbType.Int).Direction = ParameterDirection.Output
                .Parameters.Add("pRegDate", SqlDbType.VarChar, 20).Direction = ParameterDirection.Output
                .ExecuteNonQuery()
            End With
            Dim ad As New SqlDataAdapter(cmd)
            Dim dt As New DataTable

            Dim iCount As Int16
            ad.Fill(dt)
            If dt Is Nothing Then
                Return String.Empty
            End If

            If dt.Rows.Count = 0 Then
                Return String.Empty
            End If

            Dim strRequestP, strRequestO, strRequestT, strTest() As String
            ReDim strTest(0 To dt.Rows.Count - 1)
            For iCount = 0 To dt.Rows.Count - 1
                strTest(iCount) = [String].Concat("^^^", dt.Rows(iCount).Item("Alias_name"), "^")
            Next
            File.AppendAllText("C:\BuildREQuest_ELECSYS2010.txt", "|3")

            strRequestO = clsElecsys.CreatOrderRecord(1, pBarcode, String.Empty, String.Empty, strTest, DP)
            strRequestP = "P|1" 'clsELECSYS.CreatePatientRecord(1, "", strName, "", "", strDate, strSex, "", strAddress)
            strRequestT = "L|1" 'clsELECSYS.CreateTerminationRecord(1, "F")

            File.AppendAllText("C:\BuildREQuest_ELECSYS2010.txt", "|4" & strRequestT)

            strRequest = clsElecsys.CreateSinglePatientFrame(HeaderRecordCOBAS411, strRequestP, strRequestO, strRequestT)

            Return strRequest

        Catch ex As Exception

            File.AppendAllText("C:\BuildREquest_ELECSYS2010_Exception.txt", Now.ToString & vbCrLf & ex.ToString & vbCrLf & vbCrLf)

            Return String.Empty

        End Try
    End Function

    Public Shared Function SpBuildArchRequestString(ByVal pBarcode As String, _
                                        ByVal pPortID As Int16, _
                                        Optional ByVal NoOfBlock As Byte = 1, _
                                        Optional ByVal RackNo As String = "0001", _
                                        Optional ByVal CupPos As String = "01", _
                                        Optional ByVal SampleNo As String = "0000", _
                                        Optional ByVal blnRerun As Boolean = False) As String()
        Dim cmd As New SqlCommand
        Dim strRequest(3) As String
        Dim clsArch As New CI4100
        Dim strRequestP, strRequestO, strTest() As String
        Const strARCHHeaderRecord As String = "H|\^&||||||||||P|1"
        Const strRequestT As String = "L|1"
        Dim negQueryReport As String = clsArch.CreateNegQueryRecord(pBarcode)

        'Đây là barcode dùng để query trong DB
        'Nếu barcode nhận được nhỏ hơn 5 số thì sinh ra barcode mới theo format "yyMMdd01"+barcode
        Dim strTempBarcode As String = pBarcode

        Try

            If strTempBarcode.Length < 5 Then
                strTempBarcode = strTempBarcode.PadLeft(4, "0")
                strTempBarcode = Now.ToString("yyMMdd") + "01" + strTempBarcode
            End If

            Dim methodConnection As Object
            methodConnection = CType(gv_LabLinkCN, ICloneable).Clone
            Try
                methodConnection.Open()
            Catch ex As Exception
                File.AppendAllText("C:\DB_Connection_Error.txt", Now.ToString & "   spBuildARCHRequestString " & vbCrLf)
            End Try

            File.AppendAllText("C:\BuildREQuest_CI4100.txt", Now.ToString & "|" & pBarcode & "|" & pPortID)
            With cmd
                .Connection = methodConnection
                .CommandText = "spBuildRequestString_CI4100"
                .CommandType = CommandType.StoredProcedure
                .Parameters.Add("pBarcode", SqlDbType.VarChar, 50).Direction = ParameterDirection.Input
                '.Parameters("pBarcode").Value = pBarcode
                .Parameters("pBarcode").Value = strTempBarcode
                .Parameters.Add("pPortID", SqlDbType.Int).Direction = ParameterDirection.Input
                .Parameters("pPortID").Value = pPortID
                .Parameters.Add("pPatientName", SqlDbType.NVarChar, 200).Direction = ParameterDirection.Output
                .Parameters.Add("pAddress", SqlDbType.VarChar, 200).Direction = ParameterDirection.Output
                .Parameters.Add("pSex", SqlDbType.SmallInt).Direction = ParameterDirection.Output
                .Parameters.Add("pAge", SqlDbType.Int).Direction = ParameterDirection.Output
                .Parameters.Add("pRegDate", SqlDbType.VarChar, 20).Direction = ParameterDirection.Output
            End With
            Dim ad As New SqlDataAdapter(cmd)
            Dim dt As New DataTable
            File.AppendAllText("C:\BuildREQuest_CI4100.txt", "|1")

            Dim strName As String
            Dim iCount As Int16
            ad.Fill(dt)

            Try
                lablinkhelper.Utilities.WriteDataTableToFile(dt, "CI4100-" + Now.ToString("H-mm") + "-" + pBarcode + ".txt")
            Catch ex As Exception
            End Try

            If dt Is Nothing Or dt.Rows.Count = 0 Then
                Return clsArch.CreateNegQueryReportFrame(strARCHHeaderRecord, negQueryReport, strRequestT)
            End If

            strName = SpGetDatabyLength(cmd.Parameters("pPatientName").Value.ToString.Trim, 20)  'Cắt đi còn 20 ký tự
            strName = Bodau(strName)    'Hàm bỏ dấu tiếng việt

            File.AppendAllText("C:\BuildREQuest_CI4100.txt", "|2|" & strName)

            If cmd.Parameters("pSex").Value = 0 Then
            Else
            End If

            strRequestP = clsArch.CreatePatientRecord(1, "", strName)

            If dt.Rows.Count < 20 Then
                ReDim strTest(0 To dt.Rows.Count - 1)
                For iCount = 0 To dt.Rows.Count - 1
                    strTest(iCount) = [String].Concat("^^^", dt.Rows(iCount).Item("Alias_name"))
                Next iCount
                strRequestO = clsArch.CreatOrderRecord(1, pBarcode, String.Empty, String.Empty, strTest)
                strRequest = clsArch.CreateSinglePatientFrame(strARCHHeaderRecord, strRequestP, strRequestO, strRequestT)
            Else
                ReDim strTest(19)

                For iCount = 0 To 19
                    strTest(iCount) = [String].Concat("^^^", dt.Rows(iCount).Item("Alias_name"))
                Next iCount

                strRequestO = clsArch.CreatOrderRecord(1, pBarcode, String.Empty, String.Empty, strTest)

                Dim arrRequestO As New ArrayList
                arrRequestO.Add(strRequestO)

                ReDim strTest(dt.Rows.Count - 20 - 1)

                For iCount = 0 To dt.Rows.Count - 20 - 1
                    strTest(iCount) = [String].Concat("^^^", dt.Rows(iCount + 20).Item("Alias_name"))
                Next iCount

                strRequestO = clsArch.CreatOrderRecord(2, pBarcode, String.Empty, String.Empty, strTest)
                arrRequestO.Add(strRequestO)

                strRequest = clsArch.CreateSinglePatientFrame(strARCHHeaderRecord, strRequestP, arrRequestO, strRequestT)

            End If
            File.AppendAllText("C:\BuildREQuest_CI4100.txt", strRequest(2))
            Return strRequest
        Catch ex As Exception
            File.AppendAllText("C:\BuildREquest_CI4100_Error.txt", vbCrLf & ex.ToString & Now.ToString & vbCrLf & vbCrLf)
            Return clsArch.CreateNegQueryReportFrame(strARCHHeaderRecord, negQueryReport, strRequestT)
        End Try
    End Function

    Public Shared Function SpBuildIsr1000RequestString(ByVal pBarcode As String, _
                                        ByVal pPortID As Int16, _
                                        Optional ByVal NoOfBlock As Byte = 1, _
                                        Optional ByVal RackNo As String = "0001", _
                                        Optional ByVal CupPos As String = "01", _
                                        Optional ByVal SampleNo As String = "0000", _
                                        Optional ByVal blnRerun As Boolean = False) As String()
        Dim cmd As New SqlCommand
        Dim strRequest(3) As String
        Dim clsArch As New CI4100
        Dim strRequestP, strRequestO, strTest() As String
        Const strARCHHeaderRecord As String = "H|\^&||||||||||P|1"
        Const strRequestT As String = "L|1"
        Dim negQueryReport As String = clsArch.CreateNegQueryRecord(pBarcode)

        Try

            Dim methodConnection As Object
            methodConnection = CType(gv_LabLinkCN, ICloneable).Clone
            Try
                methodConnection.Open()
            Catch ex As Exception
                File.AppendAllText("C:\DB_Connection_Error.txt", Now.ToString & "   spBuildISR1000RequestString " & vbCrLf)
            End Try

            File.AppendAllText("C:\BuildREQuest_ISR1000.txt", Now.ToString & "|" & pBarcode & "|" & pPortID)
            With cmd
                .Connection = methodConnection
                .CommandText = "spBuildRequestString"
                .CommandType = CommandType.StoredProcedure
                .Parameters.Add("pBarcode", SqlDbType.VarChar, 50).Direction = ParameterDirection.Input
                .Parameters("pBarcode").Value = pBarcode
                .Parameters.Add("pPortID", SqlDbType.Int).Direction = ParameterDirection.Input
                .Parameters("pPortID").Value = pPortID
                .Parameters.Add("pPatientName", SqlDbType.NVarChar, 200).Direction = ParameterDirection.Output
                .Parameters.Add("pAddress", SqlDbType.VarChar, 200).Direction = ParameterDirection.Output
                .Parameters.Add("pSex", SqlDbType.SmallInt).Direction = ParameterDirection.Output
                .Parameters.Add("pAge", SqlDbType.Int).Direction = ParameterDirection.Output
                .Parameters.Add("pRegDate", SqlDbType.VarChar, 20).Direction = ParameterDirection.Output
            End With
            Dim ad As New SqlDataAdapter(cmd)
            Dim dt As New DataTable
            File.AppendAllText("C:\BuildREQuest_ISR1000.txt", "|1")

            Dim strName As String
            Dim iCount As Int16
            ad.Fill(dt)
            Try
                lablinkhelper.Utilities.WriteDataTableToFile(dt, "ISR1000-" + Now.ToString("H-mm") + "-" + pBarcode + ".txt")
            Catch ex As Exception

            End Try
            If dt Is Nothing Or dt.Rows.Count = 0 Then
                Return clsArch.CreateNegQueryReportFrame(strARCHHeaderRecord, negQueryReport, strRequestT)
            End If

            strName = SpGetDatabyLength(cmd.Parameters("pPatientName").Value.ToString.Trim, 20)  'Cắt đi còn 20 ký tự
            strName = Bodau(strName)    'Hàm bỏ dấu tiếng việt

            File.AppendAllText("C:\BuildREQuest_ISR1000.txt", "|2|" & strName)

            If cmd.Parameters("pSex").Value = 0 Then
            Else
            End If

            strRequestP = clsArch.CreatePatientRecord(1, "", strName)

            If dt.Rows.Count < 20 Then
                ReDim strTest(0 To dt.Rows.Count - 1)
                For iCount = 0 To dt.Rows.Count - 1
                    strTest(iCount) = [String].Concat("^^^", dt.Rows(iCount).Item("Alias_name"))
                Next iCount
                strRequestO = clsArch.CreatOrderRecord(1, pBarcode, String.Empty, String.Empty, strTest)
                strRequest = clsArch.CreateSinglePatientFrame(strARCHHeaderRecord, strRequestP, strRequestO, strRequestT)
            Else
                ReDim strTest(19)

                For iCount = 0 To 19
                    strTest(iCount) = [String].Concat("^^^", dt.Rows(iCount).Item("Alias_name"))
                Next iCount

                strRequestO = clsArch.CreatOrderRecord(1, pBarcode, String.Empty, String.Empty, strTest)

                Dim arrRequestO As New ArrayList
                arrRequestO.Add(strRequestO)

                ReDim strTest(dt.Rows.Count - 20 - 1)

                For iCount = 0 To dt.Rows.Count - 20 - 1
                    strTest(iCount) = [String].Concat("^^^", dt.Rows(iCount + 20).Item("Alias_name"))
                Next iCount

                strRequestO = clsArch.CreatOrderRecord(2, pBarcode, String.Empty, String.Empty, strTest)
                arrRequestO.Add(strRequestO)
                strRequest = clsArch.CreateSinglePatientFrame(strARCHHeaderRecord, strRequestP, arrRequestO, strRequestT)
            End If
            File.AppendAllText("C:\BuildREQuest_ISR1000.txt", strRequest(2))
            Return strRequest
        Catch ex As Exception
            'Dim f As File
            File.AppendAllText("C:\BuildREquest_ISR1000_Error.txt", vbCrLf & ex.ToString & Now.ToString & vbCrLf & vbCrLf)
            Return clsArch.CreateNegQueryReportFrame(strARCHHeaderRecord, negQueryReport, strRequestT)
        End Try
    End Function

    Public Shared Function SpBuildArchRequestStringKn(ByVal pBarcode As String, _
                                        ByVal pPortID As Int16, _
                                        Optional ByVal NoOfBlock As Byte = 1, _
                                        Optional ByVal RackNo As String = "0001", _
                                        Optional ByVal CupPos As String = "01", _
                                        Optional ByVal SampleNo As String = "0000", _
                                        Optional ByVal blnRerun As Boolean = False) As String()
        Dim cmd As New SqlCommand
        Dim strRequest(3) As String
        Dim clsArch As New CI4100
        Dim strRequestP, strRequestO, strTest() As String
        Const strArchHeaderRecord As String = "H|\^&||||||||||P|1"
        Const strRequestT As String = "L|1"
        Dim negQueryReport As String = clsArch.CreateNegQueryRecord(pBarcode)

        Try
            Dim methodConnection As Object
            methodConnection = CType(gv_LabLinkCN, ICloneable).Clone
            Try
                methodConnection.Open()
            Catch ex As Exception
                File.AppendAllText("C:\DB_Connection_Error.txt", Now.ToString & "   spBuildARCHRequestStringKN " & vbCrLf)
            End Try

            File.AppendAllText("C:\BuildREQuest_CI4100_KN.txt", Now.ToString & "|" & pBarcode & "|" & pPortID)
            With cmd
                .Connection = methodConnection
                .CommandText = "spBuildRequestString_CI4100KN"
                .CommandType = CommandType.StoredProcedure
                .Parameters.Add("pBarcode", SqlDbType.VarChar, 50).Direction = ParameterDirection.Input
                .Parameters("pBarcode").Value = pBarcode
                .Parameters.Add("pPortID", SqlDbType.Int).Direction = ParameterDirection.Input
                .Parameters("pPortID").Value = pPortID
                .Parameters.Add("pPatientName", SqlDbType.NVarChar, 200).Direction = ParameterDirection.Output
                .Parameters.Add("pAddress", SqlDbType.VarChar, 200).Direction = ParameterDirection.Output
                .Parameters.Add("pSex", SqlDbType.SmallInt).Direction = ParameterDirection.Output
                .Parameters.Add("pAge", SqlDbType.Int).Direction = ParameterDirection.Output
                .Parameters.Add("pRegDate", SqlDbType.VarChar, 20).Direction = ParameterDirection.Output
                .ExecuteNonQuery()
            End With
            Dim ad As New SqlDataAdapter(cmd)
            Dim dt As New DataTable
            File.AppendAllText("C:\BuildREQuest_CI4100_KN.txt", "|1")

            Dim strName As String
            Dim iCount As Int16
            ad.Fill(dt)
            Try
                lablinkhelper.Utilities.WriteDataTableToFile(dt, "CI4100_KN-" + Now.ToString("H-mm") + "-" + pBarcode + ".txt")
            Catch ex As Exception
            End Try

            If dt Is Nothing Or dt.Rows.Count = 0 Then
                Return clsArch.CreateNegQueryReportFrame(strArchHeaderRecord, negQueryReport, strRequestT)
            End If

            strName = SpGetDatabyLength(cmd.Parameters("pPatientName").Value.ToString.Trim, 20)  'Cắt đi còn 20 ký tự
            strName = Bodau(strName)    'Hàm bỏ dấu tiếng việt

            File.AppendAllText("C:\BuildREQuest_CI4100_KN.txt", "|2|" & strName)

            If cmd.Parameters("pSex").Value = 0 Then
            Else
            End If

            strRequestP = clsArch.CreatePatientRecord(1, "", strName)

            If dt.Rows.Count < 20 Then
                ReDim strTest(0 To dt.Rows.Count - 1)
                For iCount = 0 To dt.Rows.Count - 1
                    strTest(iCount) = [String].Concat("^^^", dt.Rows(iCount).Item("Alias_name"))
                Next iCount
                strRequestO = clsArch.CreatOrderRecord(1, pBarcode, String.Empty, String.Empty, strTest)
                strRequest = clsArch.CreateSinglePatientFrame(strArchHeaderRecord, strRequestP, strRequestO, strRequestT)
            Else
                ReDim strTest(19)

                For iCount = 0 To 19
                    strTest(iCount) = [String].Concat("^^^", dt.Rows(iCount).Item("Alias_name"))
                Next iCount

                strRequestO = clsArch.CreatOrderRecord(1, pBarcode, String.Empty, String.Empty, strTest)

                Dim arrRequestO As New ArrayList
                arrRequestO.Add(strRequestO)

                ReDim strTest(dt.Rows.Count - 20 - 1)

                For iCount = 0 To dt.Rows.Count - 20 - 1
                    strTest(iCount) = [String].Concat("^^^", dt.Rows(iCount + 20).Item("Alias_name"))
                Next iCount

                strRequestO = clsArch.CreatOrderRecord(2, pBarcode, String.Empty, String.Empty, strTest)
                arrRequestO.Add(strRequestO)

                strRequest = clsArch.CreateSinglePatientFrame(strArchHeaderRecord, strRequestP, arrRequestO, strRequestT)

            End If
            File.AppendAllText("C:\BuildREQuest_CI4100_KN.txt", strRequest(2))
            Return strRequest
        Catch ex As Exception
            'Dim f As File
            File.AppendAllText("C:\BuildREquest_CI4100_Error_KN.txt", vbCrLf & ex.ToString & Now.ToString & vbCrLf & vbCrLf)
            Return clsArch.CreateNegQueryReportFrame(strArchHeaderRecord, negQueryReport, strRequestT)
        End Try
    End Function

    Public Shared Function SpBuildRequestStringGa400(ByVal pBarcode As String, _
                                        ByVal pPortID As Int16, _
                                        ByVal pDiskID As String, _
                                        ByVal pPositionNo As String) As String()
        Dim cmd As New SqlCommand
        Dim strRequest(3) As String
        Dim clsGa400 As New GA400
        Dim nullStr() As String
        Dim strRequestP, strTest() As String
        Dim strRequestO() As String
        Const strHeaderRecord_GA400 As String = "H|\^&"
        Const strTerminatorL As String = "L|1|N"

        Try
            Dim methodConnection As Object
            methodConnection = CType(gv_LabLinkCN, ICloneable).Clone
            Try
                methodConnection.Open()
            Catch ex As Exception
                File.AppendAllText("C:\DB_Connection_Error.txt", Now.ToString & "   spBuildRequestString_GA400 " & vbCrLf)
            End Try
            File.AppendAllText("C:\BuildREQuest_GA400.txt", Now.ToString & "|" & pBarcode & "|" & pPortID)
            With cmd
                .Connection = methodConnection
                .CommandText = "spBuildRequestString_GA400"
                .CommandType = CommandType.StoredProcedure
                .Parameters.Add("pBarcode", SqlDbType.VarChar, 50).Direction = ParameterDirection.Input
                .Parameters("pBarcode").Value = pBarcode
                .Parameters.Add("pPortID", SqlDbType.Int).Direction = ParameterDirection.Input
                .Parameters("pPortID").Value = pPortID
                .Parameters.Add("pPatientName", SqlDbType.NVarChar, 200).Direction = ParameterDirection.Output
                .Parameters.Add("pAddress", SqlDbType.VarChar, 200).Direction = ParameterDirection.Output
                .Parameters.Add("pSex", SqlDbType.SmallInt).Direction = ParameterDirection.Output
                .Parameters.Add("pAge", SqlDbType.Int).Direction = ParameterDirection.Output
                .Parameters.Add("pRegDate", SqlDbType.VarChar, 20).Direction = ParameterDirection.Output
                .ExecuteNonQuery()
            End With
            Dim ad As New SqlDataAdapter(cmd)
            Dim dt As New DataTable
            File.AppendAllText("C:\BuildREQuest_GA400.txt", "|1")
            Dim strName As String
            Dim iCount As Int16
            ad.Fill(dt)
            If dt Is Nothing Or dt.Rows.Count = 0 Then
                Return nullStr
            End If
            strName = SpGetDatabyLength(cmd.Parameters("pPatientName").Value.ToString.Trim, 20)  'Cắt đi còn 20 ký tự
            strName = Bodau(strName)    'Hàm bỏ dấu tiếng việt
            File.AppendAllText("C:\BuildREQuest_GA400.txt", "|2|" & strName)
            ReDim strTest(0 To dt.Rows.Count - 1)
            For iCount = 0 To dt.Rows.Count - 1
                strTest(iCount) = [String].Concat("^^^", dt.Rows(iCount).Item("Alias_name"))
            Next
            strRequestO = clsGa400.CreatOrderRecord(1, pBarcode, pDiskID, pPositionNo, strTest)
            strRequestP = clsGa400.CreatePatientRecord(1, "", strName)
            strRequest = clsGa400.CreateSinglePatientFrame(strHeaderRecord_GA400, strRequestP, strRequestO, strTerminatorL)
            File.AppendAllText("C:\BuildREQuest_GA400.txt", strRequest(2))
            Return strRequest
        Catch ex As Exception
            File.AppendAllText("BuildREquest_GA400_Error.txt", Now.ToString & vbCrLf & ex.ToString & vbCrLf & vbCrLf)
            Return nullStr
        End Try
    End Function

    Private Shared Function SpGetDatabyLength(ByVal strInput As String, ByVal iLength As Int32) As String
        If strInput.Length >= iLength Then
            strInput = strInput.Substring(0, iLength)
        Else
            strInput = strInput.PadRight(iLength, " ")
        End If

        Return strInput
    End Function

    
End Class